package com.example.mkio.androidpractice.codes;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.mkio.androidpractice.R;

import java.lang.ref.WeakReference;

/**
 * 验证博客 http://blog.csdn.net/u013356254/article/details/52463636
 */
public class WeakReferenceAndStaticHandlerAtivity extends AppCompatActivity {

    private static class MyHandler extends Handler{
        /**
         *  Instances of static inner classes do not hold an implicit
         * reference to their outer class.
         * 静态内部类不会持有外部类的引用
         */
        private  final WeakReference<WeakReferenceAndStaticHandlerAtivity> mAtivityWeakReference;

        public MyHandler(WeakReferenceAndStaticHandlerAtivity ativity) {
            mAtivityWeakReference = new WeakReference<WeakReferenceAndStaticHandlerAtivity>(ativity);
        }

        @Override
        public void handleMessage(Message msg) {

            WeakReferenceAndStaticHandlerAtivity mAtivity = mAtivityWeakReference.get();
            if (mAtivity != null) {
                //业务
            }
        }
    }

    private MyHandler mMyHandler = new MyHandler(this);


    /**
     * Instances of anonymous classes do not hold an implicit
     * reference to their outer class when they are "static".
     */
    private static final Runnable runable = new Runnable() {
        @Override
        public void run() {
            //业务
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weak_reference_and_static_handler_ativity);
        mMyHandler.postDelayed(runable,1000*60*10);
    }
}
